package com.sqmf.action;

import com.alibaba.fastjson.JSONObject;
import com.sqmf.mapper.UserMapper;
import com.sqmf.pojo.User;
import com.sqmf.util.HttpClientUtil;
import com.sqmf.util.Result;
import com.sqmf.util.ResultCode;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.RequestMethod;

import java.net.URLDecoder;
import java.net.URLEncoder;
import java.util.List;

@RestController
@RequestMapping("/api")
public class IndexController {
    private final static Logger logger = LoggerFactory.getLogger(IndexController.class);
    private JSONObject jsonObject = null;//接收json
    @RequestMapping(value = "/test", method = RequestMethod.POST)
    public String test() {

        return "hello";
    }

    @RequestMapping(value = "/add", method = RequestMethod.POST, produces = "application/json;charset=UTF-8")
    public Result add(@RequestBody String smsReceiptJSON) {
        logger.info("*********************用户添加 HTTP POST**************************");
        logger.info("smsReceiptJSON：" + smsReceiptJSON);
        try {
            jsonObject = JSONObject.parseObject(smsReceiptJSON);
            if (
                    !jsonObject.containsKey("cipherText") ||
                            !jsonObject.containsKey("tag") ||
                            StringUtils.isEmpty(jsonObject.get("cipherText").toString()) ||
                            StringUtils.isEmpty(jsonObject.get("tag").toString())
            ) {
                logger.error(ResultCode.API_PARAM_MISS_ERROR.message());
                return Result.API_PARAM_MISS_ERROR();
            }
            return Result.SUCCESS(URLEncoder.encode(jsonObject.getString("cipherText").toString(), "UTF-8"));
        } catch (Exception $exception) {
            logger.info("desDecrypt:" + $exception.getMessage());
            return Result.API_SERVER_ERROR();
        }
    }

    @RequestMapping(value = "/push", method = RequestMethod.POST, produces = "application/json;charset=UTF-8")
    public Result push(@RequestBody String jsonStr) {
        try {
            jsonObject = JSONObject.parseObject(jsonStr);
            if (
                    !jsonObject.containsKey("url") ||
                            !jsonObject.containsKey("data") ||
                            StringUtils.isEmpty(jsonObject.get("url").toString()) ||
                            StringUtils.isEmpty(jsonObject.get("data").toString())
            ) {
                logger.error(ResultCode.API_PARAM_MISS_ERROR.message());
                return Result.API_PARAM_MISS_ERROR();
            }
            String url = jsonObject.get("url").toString();
            String jsonDataStr = jsonObject.get("data").toString();
            String result = HttpClientUtil.doPost(url, jsonDataStr, 5000, 15000);
            jsonObject = JSONObject.parseObject(result);
            return Result.SUCCESS(jsonObject);
        } catch (Exception $exception) {
            logger.error("push_error:" + $exception.getMessage());
            return Result.API_SERVER_ERROR();
        }
    }
}
